import cv2
import numpy as np


def remove_salt_and_pepper_noise(image, kernel_size=3):
    # Convert image to grayscale
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Apply median filter to remove salt and pepper noise
    denoised_image = cv2.medianBlur(gray_image, 27)

    return denoised_image


# 读取图像
image = cv2.imread("2.png")
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 去除椒盐噪声
denoised_image = remove_salt_and_pepper_noise(image)

# 显示结果
cv2.imshow("origin", gray_image)
cv2.imshow("22", denoised_image)
#cv2.imwrite(r"E:\infraVideos\20240419\fliter\3mmFilter.jpg", denoised_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
